//
//  MYGQRAddFriendVC.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/5/14.
//

import UIKit

class MYGQRAddFriendVC: MYGBaseController {
    override func viewDidLoad() {
        super.viewDidLoad()
    
        configUI()
        getQr()
    }
    
    private func getQr() {
        
        let token = MYGUserManager.user?.verification_token ?? ""
        let doctor_id = MYGUserManager.user?.doctor.id ?? 0
        
        let  urlString = "\(BaseUrl)/api/im/add_friend_qr?token=\(token)&doctor_id=\(doctor_id)"
        // http://47.93.150.30:8081/api/im/add_friend_qr?token=qAjq9iuVD5XBlynCmQ2CaOExrUHf7kB4_1708248390&doctor_id=1347"
        let url = URL(string: urlString)
    
        self.qrImage.sd_setImage(with:url)
        
//        let provider = MoyaProvider<MYGSurgicalApi>()
//        provider.request(MYGSurgicalApi.getInviteStr(params: [:])) { [self] result in
//            switch result {
//            case let .success(data):
//                let svgData: Data = data.data
//                self.qrImage.image = UIImage(data: svgData)
//                let content = String(data: svgData, encoding: .utf8)
//                self.webView.loadHTMLString(content!, baseURL: nil)
//
//            case .failure(_): break
//                
//            }
//        }
        
  
    }
    
    private func configUI() {
        self.title = "我的二维码"
        view.backgroundColor = .rgba(250, 250, 250, 1)
        view.addSubview(avatar)
        view.addSubview(nameLabel)
        view.addSubview(contentLabel)
        view.addSubview(Label)
        view.addSubview(qrImage)
        view.addSubview(bottomLabel)
        
        avatar.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(32)
            make.top.equalTo(29)
            make.width.height.equalTo(50)
        }
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(avatar.snp.right).offset(10)
            make.height.equalTo(22.5)
            make.top.equalTo(avatar)
        }
        Label.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(7)
            make.centerY.equalTo(nameLabel)
        }
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(avatar)
        }
        
        qrImage.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.height.equalTo(SCALEWIDTH(211))
            make.top.equalTo(avatar.snp.bottom).offset(40)
        }
        
        bottomLabel.snp.makeConstraints { make in
            make.top.equalTo(qrImage.snp.bottom).offset(47)
            make.left.right.equalToSuperview()
        }
        
        view.addSubview(qrTopLeftImage)
        qrTopLeftImage.snp.makeConstraints { make in
            make.width.height.equalTo(SCALEWIDTH(27))
            make.top.equalTo(qrImage.snp.top)
            make.left.equalTo(qrImage.snp.left)
        }

        view.addSubview(qrTopRightImage)
        qrTopRightImage.snp.makeConstraints { make in
            make.width.height.equalTo(SCALEWIDTH(27))
            make.top.equalTo(qrImage.snp.top)
            make.right.equalTo(qrImage.snp.right)
        }
        view.addSubview(qrBottomLeftImage)
        qrBottomLeftImage.snp.makeConstraints { make in
            make.width.height.equalTo(SCALEWIDTH(27))
            make.bottom.equalTo(qrImage.snp.bottom)
            make.left.equalTo(qrImage.snp.left)
        }
        view.addSubview(qrBottomRightImage)
        qrBottomRightImage.snp.makeConstraints { make in
            make.width.height.equalTo(SCALEWIDTH(27))
            make.bottom.equalTo(qrImage.snp.bottom)
            make.right.equalTo(qrImage.snp.right)
        }
        
        
    }
    
    // MARK: - lazy load
    
    private lazy var avatar: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 25
        imgV.sd_setImage(with: URL(string: MYGUserManager.getUser()?.doctor.avatar), placeholderImage: UIImage(named: "MYG_myPage_touxiang"), context: nil)
        return imgV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(16)
        label.textColor = .rgba(44, 44, 44, 1)
        label.text = MYGUserManager.getUser()?.doctor.name
        return label
    }()
    
    private lazy var Label: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(128, 128, 128, 1)
        label.text = MYGUserManager.getUser()?.doctor.hospital
        return label
    }()
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(128, 128, 128, 1)
        let hospital = MYGUserManager.getUser()?.doctor.department ?? ""
        let title = MYGUserManager.getUser()?.doctor.title ?? ""
        label.text = "\(hospital) \(title)"
        return label
    }()

    private lazy var qrTopLeftImage: UIImageView = {
        let imgV = UIImageView()
        imgV.image = UIImage(named: "left-top")
        return imgV
    }()
    
    private lazy var qrTopRightImage: UIImageView = {
        let imgV = UIImageView()
        imgV.image = UIImage(named: "right-top")
        return imgV
    }()
    private lazy var qrBottomLeftImage: UIImageView = {
        let imgV = UIImageView()
        imgV.image = UIImage(named: "left-bottom")
        return imgV
    }()
    private lazy var qrBottomRightImage: UIImageView = {
        let imgV = UIImageView()
        imgV.image = UIImage(named: "right-bottom")
        return imgV
    }()
    
    private lazy var qrImage: UIImageView = {
        let imgV = UIImageView()
        return imgV
    }()
    
    
    private lazy var bottomLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.text = "扫一扫上面的二维码图案，加我为好友"
        label.textColor = .rgba(128, 128, 128, 1)
        label.textAlignment = .center
        return label
    }()
    
}
